import sha1 from 'sha1'
export function signHandle(config) {
  // 添加签名头信息
  const timestamp = new Date().getTime().toString()
  let obj = { appid: '1111', timestamp: timestamp }
  let fixedObj = Object.assign({}, obj, config.params, config.data)
  // 签名数据
  if (fixedObj.hasOwnProperty.call('brinSignData')) {
    delete config.data.brinSignData
    fixedObj = Object.assign({}, obj, config.params, fixedObj.brinSignData)
  }
  // 签名
  let newData = {}
  Object.keys(fixedObj)
    .sort()
    .map((key) => {
      if (fixedObj[key] !== undefined && fixedObj[key] !== null && fixedObj[key] !== '') {
        if (['page', 'limit', 'file', 'order', 'field'].indexOf(key) === -1) {
          newData[key] = fixedObj[key]
        }
      }
    })

  config.headers['brintechapi-appid'] = '1111'
  config.headers['brintechapi-timestamp'] = timestamp
  config.headers['brintechapi-sign'] = sha1(JSON.stringify(newData) + '9HbLBnWsl83WoVXo')
  return config
}
